package yxy.game.pm2.command.union.war;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionWarMessage;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.war.UnionWarPlayerModule;

/** 联盟战 联盟战成就 */
@MessageType(proto = Command.UnionWarAch, classtype = UnionWarMessage.UnionWarAchRequest.class)
public class CMD_UnionWarAch extends Command<UnionWarMessage.UnionWarAchRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionWarMessage.UnionWarAchRequest request) {
		String playerCode = messageSource.getTag();

		UnionWarMessage.UnionWarAchResponse.Builder response = UnionWarMessage.UnionWarAchResponse.newBuilder();

		// 检查是否已经有联盟
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		{
			Result result = new UnionHandle().checkPlayerUnion(entry);
			if (!result.equals(Result.UnionIn)) {
				send(messageSource, response, result);
				return;
			}
		}
		UnionWarPlayerModule unionWarPlayerModule = UnionWarPlayerModule.get(playerCode);
		for (String taskCode : unionWarPlayerModule.getAchievement()) {
			Task task = Task.get(playerCode, taskCode);
			if (task == null) {
				continue;
			}
			response.addTasks(TaskHandle.packTask(task));
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, UnionWarMessage.UnionWarAchResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}